package cn.com.lyj6851.modules.wechat;

import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.modules.mp.domain.YdyWechatTemplate;
import cn.com.lyj6851.modules.mp.service.YdyWechatTemplateService;
import cn.com.lyj6851.modules.mp.service.dto.YdyWechatTemplateDto;
import cn.com.lyj6851.modules.mp.service.dto.YdyWechatTemplateQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(tags = "商城:微信模板管理")
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/api/ydyWechatTemplate")
public class WechatTemplateController {


    private final YdyWechatTemplateService ydyWechatTemplateService;
    private final IGenerator generator;


    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@per.check('admin','WechatTemplate:list')")
    public void download(HttpServletResponse response, YdyWechatTemplateQueryCriteria criteria) throws IOException {
        ydyWechatTemplateService.download(generator.convert(ydyWechatTemplateService.queryAll(criteria), YdyWechatTemplateDto.class), response);
    }

    @GetMapping
    @ApiOperation("查询微信模板消息")
    @PreAuthorize("@per.check('admin','WechatTemplate:list')")
    public ResponseEntity<Object> getYxWechatTemplates(YdyWechatTemplateQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(ydyWechatTemplateService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @ApiOperation("新增微信模板消息")
    @PreAuthorize("@per.check('admin','WechatTemplate:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YdyWechatTemplate resources){
        return new ResponseEntity<>(ydyWechatTemplateService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @ApiOperation("修改微信模板消息")
    @PreAuthorize("@per.check('admin','WechatTemplate:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YdyWechatTemplate resources){
        ydyWechatTemplateService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("删除微信模板消息")
    @PreAuthorize("@per.check('admin','WechatTemplate:del')")
    @DeleteMapping("/{id}")
    public ResponseEntity<Object> deleteAll(@PathVariable Integer id) {
        ydyWechatTemplateService.removeById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
